// ==UserScript==
// @name         网页图标获取器
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  获取当前网页的所有图标并提供下载(按Alt+F键激活)
// @author       Cursor
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_setClipboard
// @grant        GM_registerMenuCommand
// @icon     https://gitee.com/selling-surprises/Greasyfork-Script-repository/raw/main/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC/%E7%BD%91%E9%A1%B5%E5%9B%BE%E6%A0%87%E8%8E%B7%E5%8F%96%E5%99%A8/Web-icon.png
// ==/UserScript==

(function() {
    'use strict';

    // 添加样式
    GM_addStyle(`
        #favicon-grabber {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            z-index: 9999;
            box-shadow: 0 0 10px rgba(0,0,0,0.2);
            max-width: 300px;
            max-height: 400px;
            overflow-y: auto;
            display: none;
        }
        #favicon-grabber h3 {
            margin-top: 0;
            margin-bottom: 10px;
            font-size: 16px;
        }
        .favicon-item {
            margin-bottom: 10px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .favicon-item img {
            max-width: 32px;
            max-height: 32px;
            margin-right: 10px;
            vertical-align: middle;
            border: 1px solid #ddd;
        }
        .favicon-url {
            font-size: 12px;
            word-break: break-all;
            margin: 5px 0;
            color: #666;
        }
        .favicon-actions {
            margin-top: 5px;
        }
        .favicon-actions button {
            margin-right: 5px;
            font-size: 12px;
            padding: 2px 5px;
            cursor: pointer;
        }
        .close-button {
            position: absolute;
            top: 5px;
            right: 5px;
            cursor: pointer;
            font-size: 16px;
            color: #999;
        }
        .close-button:hover {
            color: #333;
        }
    `);

    let panel = null;

    // 注册菜单命令
    GM_registerMenuCommand('获取网页图标', togglePanel);

    // 注册快捷键 Alt+F
    document.addEventListener('keydown', function(e) {
        if (e.altKey && e.key === 'f') {
            togglePanel();
        }
    });

    function togglePanel() {
        if (panel) {
            panel.style.display = panel.style.display === 'none' ? 'block' : 'none';
        } else {
            createPanel();
            panel.style.display = 'block';
        }
    }

    function createPanel() {
        // 创建图标面板
        panel = document.createElement('div');
        panel.id = 'favicon-grabber';
        
        // 添加关闭按钮
        const closeButton = document.createElement('span');
        closeButton.className = 'close-button';
        closeButton.innerHTML = '×';
        closeButton.addEventListener('click', function() {
            panel.style.display = 'none';
        });
        
        // 添加标题
        const title = document.createElement('h3');
        title.textContent = '网页图标获取器';
        
        panel.appendChild(closeButton);
        panel.appendChild(title);
        
        // 获取所有可能的图标
        const icons = [];
        
        // 查找链接标签中的图标
        const linkElements = document.querySelectorAll('link[rel*="icon"]');
        linkElements.forEach(function(link) {
            const href = link.href;
            if (href) {
                icons.push({
                    url: href,
                    type: link.getAttribute('rel') || '未知',
                    size: link.getAttribute('sizes') || '未知'
                });
            }
        });

        // 查找网站的manifest.json中的图标
        const manifestLink = document.querySelector('link[rel="manifest"]');
        if (manifestLink && manifestLink.href) {
            fetch(manifestLink.href)
                .then(response => response.json())
                .then(data => {
                    if (data.icons && Array.isArray(data.icons)) {
                        data.icons.forEach(icon => {
                            if (icon.src) {
                                const iconUrl = new URL(icon.src, window.location.origin).href;
                                icons.push({
                                    url: iconUrl,
                                    type: 'manifest icon',
                                    size: icon.sizes || '未知'
                                });
                                updateIconsDisplay();
                            }
                        });
                    }
                })
                .catch(() => {
                    // 忽略错误
                });
        }
        
        // 添加网站根目录下可能的图标
        const rootUrl = window.location.origin;
        const commonIcons = [
            '/favicon.ico',
            '/favicon.png',
            '/apple-touch-icon.png',
            '/apple-touch-icon-precomposed.png',
            '/touch-icon-iphone.png',
            '/touch-icon-ipad.png',
            '/touch-icon-iphone-retina.png',
            '/touch-icon-ipad-retina.png',
            '/mstile-144x144.png',
            '/android-chrome-192x192.png'
        ];
        
        commonIcons.forEach(function(iconPath) {
            icons.push({
                url: rootUrl + iconPath,
                type: '可能的图标',
                size: '未知'
            });
        });
        
        // 显示图标
        updateIconsDisplay();
        
        function updateIconsDisplay() {
            // 清除现有的图标显示（除了标题和关闭按钮）
            const existingItems = panel.querySelectorAll('.favicon-item');
            existingItems.forEach(item => item.remove());
            
            if (icons.length > 0) {
                icons.forEach(function(icon) {
                    const iconContainer = document.createElement('div');
                    iconContainer.className = 'favicon-item';
                    
                    const img = document.createElement('img');
                    img.src = icon.url;
                    img.onerror = function() {
                        iconContainer.style.display = 'none';
                    };
                    
                    const info = document.createElement('span');
                    info.textContent = `类型: ${icon.type}, 尺寸: ${icon.size}`;
                    
                    const url = document.createElement('div');
                    url.className = 'favicon-url';
                    url.textContent = icon.url;
                    
                    const actions = document.createElement('div');
                    actions.className = 'favicon-actions';
                    
                    const copyButton = document.createElement('button');
                    copyButton.textContent = '复制链接';
                    copyButton.addEventListener('click', function() {
                        GM_setClipboard(icon.url);
                        copyButton.textContent = '已复制!';
                        setTimeout(function() {
                            copyButton.textContent = '复制链接';
                        }, 1000);
                    });
                    
                    const openButton = document.createElement('button');
                    openButton.textContent = '打开图标';
                    openButton.addEventListener('click', function() {
                        window.open(icon.url, '_blank');
                    });
                    
                    const downloadButton = document.createElement('button');
                    downloadButton.textContent = '下载';
                    downloadButton.addEventListener('click', function() {
                        const a = document.createElement('a');
                        a.href = icon.url;
                        a.download = icon.url.split('/').pop() || 'favicon';
                        a.target = '_blank';
                        document.body.appendChild(a);
                        a.click();
                        document.body.removeChild(a);
                    });
                    
                    actions.appendChild(copyButton);
                    actions.appendChild(openButton);
                    actions.appendChild(downloadButton);
                    
                    iconContainer.appendChild(img);
                    iconContainer.appendChild(info);
                    iconContainer.appendChild(url);
                    iconContainer.appendChild(actions);
                    
                    panel.appendChild(iconContainer);
                });
            } else {
                const noIcons = document.createElement('div');
                noIcons.textContent = '没有找到图标';
                panel.appendChild(noIcons);
            }
        }
        
        document.body.appendChild(panel);
    }
})(); 